<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button class="post-btn" @click="commentPublish">发布</van-button>
  </div>
</template>

<script>
// 我们这个接口可以给 文章添加评论， 也可以个回复添加评论
import { addCommentAjax } from "@/api/comment";
export default {
  name: "CommentPost",
  // 文章id 和 文章对应的评论数组
  props: {
    target: {
      type: [Number, String, Object],
      required: true
    },
    commentList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      message: ""
    };
  },
  created() {},
  methods: {
    // 点击发布评论
    async commentPublish() {
      try {
        const { data } = await addCommentAjax({
          target: this.target, //评论文章即为文章id，对评论进行回复则为评论id
          content: this.message, //评论内容
          art_id: null // 给文章评论，不需要传递我们的文章id，前面target已经传了
        });
        console.log(11, data);

        this.commentList.unshift(data.data.new_obj);
        console.log(this.commentList,'评论列表的数据');
        this.message = "";
        this.$emit("COMMENT_POST_EVENT");
        this.$toast('评论成功')
      } catch (err) {
        this.$toast("添加评论失败");
      }
    }
  }
};
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
